home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SOURCE.BIN / DBTstamp.java < prev    next >
Encoding:
Java Source  |  1997-06-19  |  18.8 KB  |  433 lines

  1. package symantec.itools.db.awt;
  2.  
  3. import symjava.sql.*;
  4. import symantec.itools.db.pro.*;
  5. import symantec.itools.awt.*;
  6. import java.awt.*;
  7. public class DBTstamp extends symantec.itools.awt.Tstamp implements ProjLink
  8. {
  9.  
  10.     private ProjBinder m_ProjBinder;
  11.     private String m_ScreenData;
  12.     private String m_BinderData;
  13.     private int m_treatBlankAs;
  14.     private boolean m_BinderDetermines = false;
  15.     public final static int Default    =  0;
  16.     public final static int Null       =  1;
  17.     public final static int Blank      =  2;
  18.     private String DBDF="Y*-MM-DD-hh-mm-ss-n9";
  19.     private String DBEF="YMDhmsn";
  20.     private String TYPE="Timestamp";
  21.     public static  String Time="Time";
  22.     public static  String Timestamp="Timestamp";
  23.     public static  String Date="Date";
  24.     public DBTstamp()
  25.     {
  26.         super();
  27.  
  28.         m_BinderData = new String();
  29.         m_ScreenData = new String();
  30.  
  31.     }
  32.    public void setDBDF(String form)
  33.     {
  34.         DBDF=form;
  35.     }
  36.     public String getDBDF()
  37.     {
  38.         return(DBDF);
  39.     }
  40.     public void setDBEF(String form)
  41.     {
  42.         DBEF=form;
  43.     }
  44.     public String getDBEF()
  45.     {
  46.         return(DBEF);
  47.     }
  48.     public String getType()
  49.     {
  50.         return(TYPE);
  51.     }
  52.     public void setType(String type)
  53.     {
  54.  
  55.         TYPE=type;
  56.         if(type.equals("Date")){
  57.  
  58.             this.setDisplayFormat("D* M* D%TH, Y*");
  59.             this.setEntryFormat("MDY");
  60.             setDBDF("Y*-MM-DD");
  61.             setDBEF("YMD");
  62.         }
  63.         if(type.equals("Timestamp")){
  64.             this.setDisplayFormat("D* M* D%TH, Y* H%:m%:s%,n3 AM");
  65.             this.setEntryFormat("MDYhmsn");
  66.             setDBDF("Y*-MM-DD-hh-mm-ss-n9");
  67.             setDBEF("YMDhmsn");
  68.         }
  69.         if(type.equals("Time")){
  70.             this.setDisplayFormat("H%:m%:s% AM");
  71.             this.setEntryFormat("hms");
  72.             setDBDF("hh-mm-ss");
  73.             setDBEF("hmsn");
  74.         }
  75.     }
  76.     public void init(ProjBinder binder)
  77.     {
  78.         m_ProjBinder = binder;
  79.         setEditable(m_ProjBinder);
  80.     }
  81.  
  82.     //*************************************************************************//
  83.     //                                                                         //
  84.     // FUNCTION: setBinding                                                    //
  85.     //                                                                         //
  86.     //  PURPOSE:                                                               //
  87.     //                                                                         //
  88.     // PARAMETERS:                                                             //
  89.     //                                                                         //
  90.     //  RETURNS:                                                               //
  91.     //                                                                         //
  92.     // COMMENTS:                                                               //
  93.     //                                                                         //
  94.     //*************************************************************************//
  95.  
  96.     public void setBinding(RelationView relView, String projection) throws SQLException
  97.     {
  98.         try {
  99.             int projectionNumber = relView.findProjByName(projection);
  100.                relView.bindProj(projectionNumber, this);
  101.         }
  102.         catch (SQLException Ex)
  103.         {
  104.             super.raiseException(
  105.                 "SQLException from setBinding: "
  106.                 + Ex.getMessage());
  107.         }
  108.     }
  109.  
  110.     //*************************************************************************//
  111.     //                                                                         //
  112.     // FUNCTION: setDynamicUpdate                                              //
  113.     //                                                                         //
  114.     //  PURPOSE:                                                               //
  115.     //                                                                         //
  116.     // PARAMETERS:                                                             //
  117.     //                                                                         //
  118.     //  RETURNS:                                                               //
  119.     //                                                                         //
  120.     // COMMENTS:                                                               //
  121.     //                                                                         //
  122.     //*************************************************************************//
  123.  
  124.     public void setDynamicUpdate(boolean update)
  125.     {
  126.     }
  127.  
  128.      //*************************************************************************//
  129.     //                                                                         //
  130.     // FUNCTION: setTreatBlankAs                                               //
  131.     //                                                                         //
  132.     //  PURPOSE:                                                               //
  133.     //                                                                         //
  134.     // PARAMETERS:                                                             //
  135.     //                                                                         //
  136.     //  RETURNS:                                                               //
  137.     //                                                                         //
  138.     // COMMENTS:                                                               //
  139.     //                                                                         //
  140.     //*************************************************************************//
  141.  
  142.     
  143.     public void setTreatBlankAs(int treatAs)
  144.     {
  145.         switch(treatAs)
  146.         {
  147.             case Default:
  148.             {
  149.                 m_treatBlankAs = RelationView.SETBLANKTODEFAULT;
  150.             }
  151.             case Null:
  152.             {
  153.                 m_treatBlankAs = RelationView.SETBLANKTONULL;
  154.             }
  155.             case Blank:
  156.             {
  157.                 m_treatBlankAs = RelationView.SETBLANKTOEMPTY;
  158.             }
  159.         }
  160.     }
  161.     //*************************************************************************//
  162.     //                                                                         //
  163.     // FUNCTION: notifyDataChange                                              //
  164.     //                                                                         //
  165.     //  PURPOSE:                                                               //
  166.     //    Implement the ProjLink interface function, notifyDataChange().       //
  167.     //    This function is called when the projection's data changes at        //
  168.     //    the database.                                                        //
  169.     //                                                                         //
  170.     // PARAMETERS:                                                             //
  171.     //  @param binder The ProjBinder binder object which this component can use//
  172.     //                to get or change data at the database server.            //
  173.     //                                                                         //
  174.     //  RETURNS:                                                               //
  175.     //                                                                         //
  176.     // COMMENTS:                                                               //
  177.     //                                                                         //
  178.     //  Step 1: set m_ProjBinder equal to binder object                        //
  179.     //  Step 2: synchronize m_BinderData with binder object                    //
  180.     //  Step 3: synchronize m_ScreenData with current screen                   //
  181.     //  Step 4: if the two are different then                                  //
  182.     //       - set m_ScreenData equal to m_BinderData to avoid recursion       //
  183.     //       - call setText to update screen                                   //
  184.     //  Step 5: enable or disable editing based on binder                      //
  185.     //                                                                         //
  186.     //*************************************************************************//
  187.  
  188.     public void notifyDataChange(ProjBinder binder)
  189.     {
  190.         m_BinderData = "";
  191.         if (binder == null)
  192.         {
  193.             return;
  194.         }
  195.         m_ProjBinder = binder;
  196.  
  197.         try
  198.         {
  199.             if (binder.getRelationView().getCurrentRecordState()
  200.                 != Record.RECSTATE_INVALID)
  201.             {
  202.                 if (binder.isReadable() && !binder.isNull())
  203.                 {
  204.                     if(TYPE.equals("Date"))
  205.                     m_BinderData = binder.getDate().toString();
  206.                     if(TYPE.equals("Time"))
  207.                     m_BinderData = binder.getTime().toString();
  208.                     if(TYPE.equals("Timestamp"))
  209.                     m_BinderData = binder.getTimestamp().toString();
  210.                 }
  211.             }
  212.         }
  213.         catch (SQLException Ex)
  214.         {
  215.             raiseException(
  216.                 "SQLException from TextField.notifyDataChange.getString: "
  217.                 + Ex.getMessage());
  218.         }
  219.         if (m_BinderData == null)
  220.         {
  221.             m_BinderData = new String();
  222.  
  223.         }
  224.  
  225.         m_ScreenData = getText();
  226.        
  227.         if (!m_BinderData.equals(m_ScreenData))
  228.         {
  229.             m_ScreenData = m_BinderData;
  230.             if(m_BinderData.length()==0){
  231.                 setText("");
  232.                 }
  233.             else{
  234.             readDate(m_ScreenData,DBEF);
  235.             setText(exTstamp(getDisplayFormat()));
  236.             }
  237.         }
  238.  
  239.         if (m_BinderDetermines)
  240.         {
  241.             setEditable(m_ProjBinder);
  242.         }
  243.     }
  244.  
  245.     //*************************************************************************//
  246.     //                                                                         //
  247.     // FUNCTION: notifyInputChange                                             //
  248.     //                                                                         //
  249.     //  PURPOSE: Handle any event that causes us to send data to binder object //
  250.     //                                                                         //
  251.     //  PARAMETERS:                                                            //
  252.     //                                                                         //
  253.     //  RETURNS:                                                               //
  254.     //                                                                         //
  255.     // COMMENTS:                                                               //
  256.     //                                                                         //
  257.     //                                                                         //
  258.     //  Step 1: synchronize m_ScreenData with current screen                   //
  259.     //  Step 2: if m_ScreenData differs from m_BinderData then...              //
  260.     //       - set m_BinderData equal to m_ScreenData to avoid recursion       //
  261.     //       - call m_Binder.setString to update binder                        //
  262.     //                                                                         //
  263.     //*************************************************************************//
  264.  
  265.     boolean notifyInputChanged(String input)
  266.     {
  267.         if (!m_ScreenData.equals(input))
  268.         {
  269.             m_ScreenData = input;
  270.         }
  271.         try{
  272.             Tstamp TS= new Tstamp(m_BinderData,DBEF);
  273.             
  274.         if (!TS.getTstamp(getDisplayFormat()).equals(m_ScreenData)
  275. && (m_ProjBinder.getRelationView().getCurrentRecordState() != Record.RECSTATE_INVALID)
  276. && (m_ProjBinder.getRelationView().getCurrentRecordState() != Record.RECSTATE_DELETED)
  277. && (m_ProjBinder.getRelationView().getCurrentRecordState() != Record.RECSTATE_DB_DELETED)
  278. )
  279.         {
  280.  
  281.             
  282.             m_BinderData = m_ScreenData;
  283.             readDate(m_BinderData,getDisplayFormat());
  284.  
  285.             try
  286.                 {
  287.  
  288.             if(m_ScreenData.trim().length()!=0){
  289.                 if (TYPE.equals("Date"))
  290.                 m_ProjBinder.setDate(new symjava.sql.Date(year-1900,month,day));
  291.                 if (TYPE.equals("Time")) 
  292.                 m_ProjBinder.setTime(new symjava.sql.Time(hour,minute,second));
  293.                 if (TYPE.equals("Timestamp"))
  294.                 m_ProjBinder.setTimestamp(new symjava.sql.Timestamp(year-1900,month,day,hour,minute,second,nanosec));
  295.             }
  296.             if(m_ScreenData.trim().length()==0&&!m_ProjBinder.isNull()){
  297.                 if (TYPE.equals("Date"))
  298.                 m_ProjBinder.setNull(Types.DATE);
  299.                 if (TYPE.equals("Time")) 
  300.                 m_ProjBinder.setNull(Types.TIME);
  301.                 if (TYPE.equals("Timestamp"))
  302.                 m_ProjBinder.setNull(Types.TIMESTAMP);
  303.                }
  304.             }
  305.             catch (SQLException Ex)
  306.             {
  307.                 raiseException(
  308.                     "SQLException from TextField.notifyInputChange: "
  309.                     + Ex.getMessage());
  310.                 return false;
  311.             }
  312.         }
  313.         }catch (SQLException Ex)
  314.             {
  315.                 raiseException(
  316.                     "SQLException from TextField.notifyInputChange: "
  317.                     + Ex.getMessage());
  318.                 return false;
  319.             }
  320.         return true;
  321.     }
  322.  
  323.     //*************************************************************************//
  324.     //                                                                         //
  325.     // FUNCTION: notifySetData                                                 //
  326.     //                                                                         //
  327.     //  PURPOSE: Send data to binder                                           //
  328.     //                                                                         //
  329.     //  PARAMETERS:                                                            //
  330.     //  @param binder The ProjBinder binder object which this component can use//
  331.     //                to change data at the database server.                   //
  332.     //                                                                         //
  333.     //  RETURNS:                                                               //
  334.     //                                                                         //
  335.     // COMMENTS:                                                               //
  336.     //                                                                         //
  337.     //                                                                         //
  338.     //*************************************************************************//
  339.  
  340.     public boolean notifySetData(ProjBinder binder) throws SQLException
  341.     {
  342.         return ( notifyInputChanged(getText()) );
  343.     }
  344.  
  345.     //*************************************************************************//
  346.     //                                                                         //
  347.     // FUNCTION: setEditable                                                   //
  348.     //                                                                         //
  349.     //  PURPOSE: Enable or disable input                                       //
  350.     //                                                                         //
  351.     //  PARAMETERS:                                                            //
  352.     //  @param binder The ProjBinder binder object which this component can use//
  353.     //                to change data at the database server.                   //
  354.     //                                                                         //
  355.     //  RETURNS:                                                               //
  356.     //                                                                         //
  357.     // COMMENTS:                                                               //
  358.     //                                                                         //
  359.     //*************************************************************************//
  360.  
  361.     void setEditable(ProjBinder binder)
  362.     { 
  363.         //Disable input if binder is not writeable
  364.         boolean isWritable = false;
  365.  
  366.         try
  367.         {
  368.             if (binder != null)
  369.             {
  370.                 RelationView rv = binder.getRelationView();
  371.                 if (rv != null && (rv.getCurrentRecordState()
  372.                                          != Record.RECSTATE_INVALID))
  373.                 {
  374.                     isWritable = binder.isWritable();
  375.                 }
  376.             }
  377.         }
  378.         catch (SQLException Ex)
  379.         {
  380.             raiseException(
  381.                 "SQLException from setEditable: "
  382.                 + Ex.getMessage());
  383.         }
  384.         super.setEditable(isWritable);
  385.     }
  386.  
  387.     //*************************************************************************//
  388.     //                                                                         //
  389.     // FUNCTION: setEditable                                                   //
  390.     //                                                                         //
  391.     //  PURPOSE: Set editable attribute                                        //
  392.     //                                                                         //
  393.     //  PARAMETERS:                                                            //
  394.     //                                                                         //
  395.     //  RETURNS:                                                               //
  396.     //                                                                         //
  397.     // COMMENTS:                                                               //
  398.     //                                                                         //
  399.     //*************************************************************************//
  400.  
  401.     public void setEditable(boolean value)
  402.     {
  403.         m_BinderDetermines = value;
  404.         super.setEditable(value);
  405.     }
  406.  
  407.     public void setDisplayFormat(String dispFmt)
  408.     {
  409.         super.setDisplayFormat(dispFmt);
  410.         notifyDataChange(m_ProjBinder);
  411.     }
  412.      public boolean handleEvent(Event event) {
  413.         if(event.id==Event.ACTION_EVENT||event.id==Event.LOST_FOCUS)
  414.         {
  415.             if(event.id==Event.ACTION_EVENT&&getText().trim().length()<3)
  416.            {
  417.                if(m_BinderData.length()==0)
  418.                setTstamp(new java.util.Date());
  419.                 printTstamp();
  420.                 notifyInputChanged(getText());
  421.                 }
  422.             if(getText().trim().length()>=3)
  423.            {
  424.                 setTstamp(getText(),getEntryFormat());
  425.                 printTstamp();
  426.                 notifyInputChanged(getText());
  427.                 }
  428.         }
  429.     return(false);
  430.     
  431.     }
  432. }
  433.